package com.cl.short_video_java.web.role;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cl.short_video_java.annotation.IsRole;
import com.cl.short_video_java.annotation.Log;
import com.cl.short_video_java.annotation.Login;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.menum.BusinessType;
import com.cl.short_video_java.pojo.SysMenu;
import com.cl.short_video_java.pojo.VideoRole;
import com.cl.short_video_java.service.SysMenuService;
import com.cl.short_video_java.service.VideoRoleService;
import com.cl.short_video_java.vo.role.Role;
import com.cl.short_video_java.vo.role.RoleSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 付
 * @Date 2024-11-07 17:52
 * @Version 1.0
 * @description
 * @since 1.0
 */

@RestController
@RequestMapping("/role")
public class RoleController {

	private VideoRoleService videoRoleService;
	private SysMenuService sysMenuService;

	@Autowired
	RoleController(VideoRoleService videoRoleService, SysMenuService sysMenuService) {
		this.videoRoleService = videoRoleService;
		this.sysMenuService = sysMenuService;
	}

	/**
	 * 根据搜索条件获取角色信息
	 *
	 * @param roleSearch
	 * @return
	 */
	@PostMapping("/list")
	public Result getList(@RequestBody RoleSearch roleSearch) {
		List<VideoRole> list = videoRoleService.search(roleSearch);
		return Result.success(list);
	}

	/**
	 * 新增或修改
	 *
	 * @param role
	 * @return
	 */
	@PostMapping("/save")
	@Log(title = "角色修改", businessType = BusinessType.UPDATE)
	@IsRole(menuId = 35)
	public Result insertUpdate(@Validated @RequestBody Role role, HttpServletRequest request) {
		videoRoleService.saveOrUpdate(role, request);
		return Result.success();
	}

	/**
	 * 获取对应用户的menu信息
	 *
	 * @param request
	 * @return
	 */
	@GetMapping("/menu")
	@Login(issuer = Const.GRADE_ADMIN)
	public Result getMenu(HttpServletRequest request) {
		List<SysMenu> list = sysMenuService.roleMenu(request);
		return Result.success(list);
	}


	/**
	 * 删除角色
	 *
	 * @param ids
	 * @return
	 */
	@PostMapping("/del")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 36)
	@Log(title = "角色删除", businessType = BusinessType.DELETE)
	public Result del(@RequestBody List<Integer> ids) {
		videoRoleService.del(ids);
		return Result.success();
	}

	/**
	 * 设置用户角色
	 *
	 * @param id
	 * @param roleId
	 * @return
	 */
	@GetMapping("/user/set")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 37)
	@Log(title = "权限设置", businessType = BusinessType.UPDATE)
	public Result setUser(@RequestParam("userId") Integer id, @RequestParam("roleId") Integer roleId) {
		videoRoleService.setUser(id, roleId);
		return Result.success();
	}

	@GetMapping("/cancel")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 37)
	@Log(title = "权限取消", businessType = BusinessType.UPDATE)
	public Result cancelRole(@RequestParam("userId") Integer id) {
		videoRoleService.cancel(id);
		return Result.success();
	}

	@GetMapping("/set/list")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 37)
	public Result setList() {
		List<VideoRole> list = videoRoleService.list(new QueryWrapper<VideoRole>().ne("role_id", 1));
		return Result.success(list);
	}

	@GetMapping("/limit")
	@Login(issuer = Const.GRADE_ADMIN)
	@IsRole(menuId = 38)
	@Log(title = "角色状态更改", businessType = BusinessType.UPDATE)
	public Result limit(@RequestParam("rid") Integer rid) {
		videoRoleService.limit(rid);
		return Result.success();
	}
}
